package com.gxyk.view;

import com.example.jiemian.R;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;


public class SpinnerPopupWindow extends PopupWindow {
	private Context context;
	private LinearLayout container;
	private OnItemClickListener_spinner listener;
	private int num;
	private int minNum;

	public SpinnerPopupWindow(Context context) {
		super(context);
		this.context = context;
		initView();
	}

	private void initView() {
		View view = View.inflate(context, R.layout.popwindow_layout, null);
		container = (LinearLayout) view.findViewById(R.id.container);
		setContentView(view);
	}

	public void setNum(int num, int minNum) {
		if (this.num != num || this.minNum != minNum) {
			container.removeAllViews();
			for (int i = minNum + 1; i <= num; i++) {
				TextView textView = (TextView) View.inflate(context, R.layout.spinner_item, null);	
				textView.setText(String.valueOf(i));
				final int index = i;
				textView.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (listener != null) {
							listener.onItemClick(index);
						}
					}
				});
				container.addView(textView);
			}
		}
		this.num = num;
		this.minNum = minNum;
	}

	public void setOnItemClickListener(OnItemClickListener_spinner listener) {
		this.listener = listener;
	}

	public interface OnItemClickListener_spinner {
		void onItemClick(int index);
	}
}
